#ifndef Timer_First
#define Timer_First
#ifdef __GNUG__
#pragma interface
#endif

#include "SysEvtHandler.h"
#include "Time.h"

class System;
class TOHandler;
struct tm;

enum {
	eSync= TRUE,
	eAsync= FALSE
};

//---- Timer -------------------------------------------------------------------

class Timer : public SysEvtHandler {
public:
	MetaDef(Timer);

	Timer(long msec, bool syncmode= eSync);
		// sync == TRUE: Notify is called only from the event loop
		// sync == FALSE: Notify is called asynchronous
	virtual ~Timer();
	void Install();
		// install timer; insert into timer queue
	bool UnInstall();
		// remove from timer queue; returns TRUE if found
	virtual void Reset();
		// reset timer value to initial value

	Time GetTime() const;
	Time GetAbsTime() const;
	Timer *GetNext() const;
	bool WasTimeout() const;
	bool IsSync() const;
	bool IsAsync() const;

	bool CheckTimer(Timer **head, const Time &now);

protected:
	virtual bool Notify();

public:
friend System;
	Timer *next;
	Time time, abstime;
	bool syncmode, timeout;
};

inline Time Timer::GetTime() const
	{ return time; }
inline Time Timer::GetAbsTime() const
	{ return abstime; }
inline Timer *Timer::GetNext() const
	{ return next; }
inline bool Timer::WasTimeout() const
	{ return timeout; }
inline bool Timer::IsSync() const
	{ return syncmode == eSync; }
inline bool Timer::IsAsync() const
	{ return syncmode == eAsync; }

//---- ObjTime -----------------------------------------------------------------

class ObjTime: public Object {
	TOHandler *updatehandler;
	u_long now;
	struct tm *l;
public:
	MetaDef(ObjTime);
	ObjTime();
	~ObjTime();
	void GetHMS(int *h, int *m, int *s);
	void Init();
	void Update();
	char *Format(const char *fmt, char *buf= 0, int len= 0);
};

extern ObjTime gTime;

#endif

